package com.yidianyanglao.common.mapper;

import com.yidianyanglao.common.bean.*;
import com.yidianyanglao.core.bean.ErrorReocrd;
import com.yidianyanglao.core.bean.MobileCode;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by wb on 2016/10/28.
 */
@Repository
public interface CommonMapper {

    /**
     * 根据账号和密码获取用户id
     * @param account
     * @param password
     * @return
     */
    @Select("SELECT id FROM yl_user WHERE id = #{account} or mobile = #{account} AND password = #{password}")
    String getUserId(@Param("account") String account, @Param("password") String password);

    /**
     * 插入一条验证码记录
     * @param mobileCode
     * @return
     */
    @Insert("INSERT INTO yl_mobilecode" +
            "(mobile,code,createdate,enddate,state)" +
            "VALUES(#{mobile},#{code},NOW(),DATE_ADD(NOW(),INTERVAL 5 MINUTE),0)")
    int addMobileCode(MobileCode mobileCode);

    /**
     * 根据电话号码获取过去五分钟内是否存在验证码
     * @param mobile
     * @return
     */
    @Select("SELECT TIMESTAMPDIFF(SECOND, createdate, NOW()) AS `second` FROM yl_mobilecode where mobile =#{mobile} AND state = 0 order by createdate DESC LIMIT 0,1")
    String chenkMobileCodeHas(@Param("mobile")String mobile);

    /**
     * 根据账号检查用户是否存在并获取部分用户信息
     * @param account
     * @return
     */
    @Select("SELECT mobile,realname,id FROM yl_user WHERE id = #{account} or mobile=#{account}")
    Map<String,Object> checkUserHas(String account);

    /**
     * 根据电话号码和验证码获取一条验证码数据
     * @param mobile
     * @param code
     * @return
     */
    @Select("SELECT id,mobile,code,createdate,enddate,state " +
            "FROM yl_mobilecode " +
            "WHERE " +
            "mobile=#{mobile} AND code=#{code} AND enddate>NOW() AND state=0 " +
            "order by createdate DESC " +
            "limit 0,1")
    MobileCode getMobileCode(@Param("mobile")String mobile, @Param("code") String code);


    /**
     * 根据id修改验证码使用状态
     * @param id
     * @return
     */
    @Update("UPDATE yl_mobilecode " +
            "SET state = 1 " +
            "WHERE id=#{id}")
    int updateMobileCode(@Param("id")int id);


    /**
     * 获取banner数据列表
     * @return
     */
    @Select("SELECT * FROM yl_banner ORDER BY `sort`")
    List<Banner> getBannerList();

    /**
     * 查询系统数据
     * @return
     */
    //@Select("SELECT * FROM yl_platform_data WHERE TO_DAYS( NOW() )-TO_DAYS(createdate) <=1")
    @Select("SELECT * FROM yl_platform_data ORDER BY createdate DESC , series_id  LIMIT 2")
    List<PlatformData> getSysDate();

    /**
     * 类型累计销售和
     * @param type
     * @return
     */
    @Select("SELECT IFNULL(SUM(totalsales),0) FROM yl_platform_data WHERE series_id = #{type}")
    double getAlltotalsales(@Param("type")String type);


    /**
     * 定义消息添加
     * @param notice
     * @return
     */
    @Insert("INSERT INTO yl_notice (user_id,user_name,content,createdate,noticetype,readstate)" +
            "VALUES(#{userId},#{userName},#{content},NOW(),#{noticetype},#{readstate})")
    int addNotice(Notice notice);


    /**
     * 异常列表
     * @param errorLog
     * @return
     */
    @Insert("INSERT INTO yl_errorLog(createdate,message,stack,errorname)VALUES(NOW(),#{message},#{stack},#{errorname})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int addErrorLog(ErrorLog errorLog);


    /*
     *  记录支付密码异常
     */
    @Insert("INSERT INTO yl_error_record (`type`,user_id,createdate,ip,content)VALUES(#{type},#{userId},NOW(),#{ip},#{content})")
    int addErrorRecord(ErrorReocrd errorRecord);

    /**
     * 查询今天的异常记录数据
     * @param type
     * @param userId
     * @return
     */
    @Select("SELECT COUNT(1) FROM yl_error_record WHERE `type`=#{type} AND user_id=#{userId} AND createdate > #{today}")
    int getErrorRecordCount(@Param("type")int type, @Param("userId")int userId, @Param("today")Date today);

    /**
     * 查询APP首页功能菜单
     * @return
     */
    @Select("SELECT * FROM yl_function_menu WHERE `online` = 0 ORDER BY sort DESC")
    List<FunctionMenu> getFunctionMenu();

    /**
     * 获取菜单版本号验证
     * @param version
     * @return
     */
    @Select("SELECT COUNT(1) FROM yl_function_menu WHERE version = #{version}")
    int getFunctionMenuCount(@Param("version") String version);

    /**
     * 获取一下最近一天产生计算的时间
     * @return
     */
    @Select("SELECT createdate FROM yl_platform_data ORDER BY createdate DESC LIMIT 1")
    Date getPlatformDataDate();

    /**
     * https协议变更时间
     * @return
     */
    @Select("SELECT date FROM yl_time WHERE `type` = 1")
    Date getUpdateHttpsTimes();


}
